package com.playball.lottery.frm.yhzx;

import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.playball.lottery.R;
import com.playball.lottery.base.BaseActivity;
import com.playball.lottery.customview.ShowTipDialog;
import com.playball.lottery.nt.GetRequestParams;


public class JddVerificationActivity extends BaseActivity implements OnClickListener {

	private TextView tv_title;
	private TextView btn_title_left;
	private TextView text_time;
	private Button but_again_code;
	private Button btn_next;
	private ShowTipDialog showdialog = new ShowTipDialog();
	private int time = 60;
	private Handler handler = new Handler();
	private LinearLayout lin_time;
	private Timer mTimer;
	private EditText et_verification;
	private TextView tex_username, tex_mobile;
	private String username;
	private String mobile;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.jdd_find_pass_verification);
		Intent intent = getIntent();
		username = intent.getStringExtra("username");
		mobile = intent.getStringExtra("mobile");
		tv_title = (TextView) findViewById(R.id.top_title_info);
		btn_title_left = (TextView) findViewById(R.id.top_title_back);
		text_time = (TextView) findViewById(R.id.text_time);
		lin_time = (LinearLayout) findViewById(R.id.lin_time);
		but_again_code = (Button) findViewById(R.id.but_again_code);
		btn_next = (Button) findViewById(R.id.btn_next);
		et_verification = (EditText) findViewById(R.id.et_verification);
		tex_username = (TextView) findViewById(R.id.tex_username);
		tex_mobile = (TextView) findViewById(R.id.tex_mobile);
		tex_username.setText(username);
		tex_mobile.setText(mobile);
		tv_title.setText("输入验证码");
		btn_title_left.setOnClickListener(this);
		but_again_code.setOnClickListener(this);
		btn_next.setOnClickListener(this);
		mTimer = new Timer();
		mTimer.schedule(new MyTimer(), 0, 1000);

	}

	// 定时器
	class MyTimer extends TimerTask {

		@Override
		public void run() {
			handler.post(new Runnable() {
				@Override
				public void run() {
					text_time.setText(time + "");
					if (time == 0) {
						lin_time.setVisibility(View.INVISIBLE);
						but_again_code.setVisibility(View.VISIBLE);
						mTimer.cancel();
					}
					time--;
				}
			});
		}

	}

	// 点击下一步校验验证码
	public void Verification() {
		try {
			// close keyboard
			InputMethodManager inputmanger = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
			inputmanger.hideSoftInputFromWindow(et_verification.getWindowToken(), 0);
			if (et_verification.getText().toString().equals("")) {
				Toast.makeText(JddVerificationActivity.this, R.string.input_validation_code, Toast.LENGTH_SHORT).show();
			} else {
				AsyncLoader task = new AsyncLoader();
				task.execute("1032");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public class AsyncLoader extends AsyncTask<String, Integer, String> {// 继承AsyncTask

		// 1.准备运行
		protected void onPreExecute() {
			showdialog.showDialog(JddVerificationActivity.this);
		}

		// 2.正在后台运行
		@Override
		protected String doInBackground(String... params) {
			String strResult;
			try {
				JSONObject param = new JSONObject();
				param.put("username", username);
				param.put("verifycode", et_verification.getText().toString());
				String action = params[0];
				// 获取请求返回值
				strResult = GetRequestParams.getRequestResult(param.toString(), action, getApplicationContext());
				return strResult;
			} catch (Exception e) {
				// TODO: handle exception
				Log.e("error", e.toString());
				return null;
			}
		}

		// 完成后台任务
		protected void onPostExecute(String result) {
			if (result != null) {
				try {

					System.out.println("result = " + result);

					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					// 接下来的就是JSON对象的操作了
					if (Integer.parseInt(js.getString("Code")) == 0) {
						Intent intent = new Intent();
						intent.setClass(JddVerificationActivity.this, JddAlterPassActivity.class);
						intent.putExtra("username", username);
						intent.putExtra("mobile", mobile);
						startActivity(intent);
						finish();
					}
					Toast.makeText(JddVerificationActivity.this, js.getString("Data"), Toast.LENGTH_SHORT).show();
				} catch (JSONException ex) {
					// 异常处理代码
					Log.e("error", ex.toString());
				} finally {
					showdialog.closeDialog();
				}
			} else {
				Toast.makeText(JddVerificationActivity.this, getString(R.string.problem_01), Toast.LENGTH_LONG).show();
			}
		}

	}

	public void onResume() {
		super.onResume();
		
	}

	public void onPause() {
		super.onPause();
		
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// setResult(RESULT_CANCELED);
			this.finish();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.top_title_back:
			finish();
			break;
		case R.id.but_again_code:
			AsyncLoader2 task = new AsyncLoader2();
			task.execute("1031");
			break;
		case R.id.btn_next:
			Verification();
			break;
		default:
			break;
		}

	};

	// 重新获取验证码
	public class AsyncLoader2 extends AsyncTask<String, Integer, String> {// 继承AsyncTask

		// 1.准备运行
		protected void onPreExecute() {
			showdialog.showDialog(JddVerificationActivity.this);
		}

		// 2.正在后台运行
		@Override
		protected String doInBackground(String... params) {
			String strResult;
			try {
				JSONObject param = new JSONObject();
				param.put("username", username);
				param.put("mobile", mobile);
				String action = params[0];
				// 获取请求返回值
				strResult = GetRequestParams.getRequestResult(param.toString(), action, getApplicationContext());
				return strResult;
			} catch (Exception e) {
				// TODO: handle exception
				Log.e("error", e.toString());
				return null;
			}
		}

		// 完成后台任务
		protected void onPostExecute(String result) {
			if (result != null) {
				try {

					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					// 接下来的就是JSON对象的操作了
					if (Integer.parseInt(js.getString("Code")) == 0) {
						time = 60;
						mTimer = new Timer();
						mTimer.schedule(new MyTimer(), 0, 1000);
						lin_time.setVisibility(View.VISIBLE);
						but_again_code.setVisibility(View.INVISIBLE);
					}
					Toast.makeText(JddVerificationActivity.this, js.getString("Data"), Toast.LENGTH_SHORT).show();
				} catch (JSONException ex) {
					// 异常处理代码
					Log.e("error", ex.toString());
				} finally {
					showdialog.closeDialog();
				}
			} else {
				Toast.makeText(JddVerificationActivity.this, getString(R.string.problem_01), Toast.LENGTH_LONG).show();
			}
		}

	}
}
